package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.PositionDataMapper;
import com.ruoyi.system.domain.PositionData;
import com.ruoyi.system.service.IPositionDataService;

/**
 * 岗位标准Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-30
 */
@Service
public class PositionDataServiceImpl implements IPositionDataService
{
    @Autowired
    private PositionDataMapper positionDataMapper;

    /**
     * 查询岗位标准
     *
     * @param standardPosition 岗位标准主键
     * @return 岗位标准
     */
    @Override
    public PositionData selectPositionDataByStandardPosition(String standardPosition)
    {
        return positionDataMapper.selectPositionDataByStandardPosition(standardPosition);
    }

    /**
     * 查询岗位标准列表
     *
     * @param positionData 岗位标准
     * @return 岗位标准
     */
    @Override
    public List<PositionData> selectPositionDataList(PositionData positionData)
    {
        List<PositionData> positionData1 = positionDataMapper.selectPositionDataList(positionData);
        for (PositionData data : positionData1) {
            String pathName = positionDataMapper.getPathName(data.getPromotionPath());
            data.setPromotionPathName(pathName);
        }
        return positionData1;
    }

    /**
     * 新增岗位标准
     *
     * @param positionData 岗位标准
     * @return 结果
     */
    @Override
    public int insertPositionData(PositionData positionData)
    {
        return positionDataMapper.insertPositionData(positionData);
    }

    /**
     * 修改岗位标准
     *
     * @param positionData 岗位标准
     * @return 结果
     */
    @Override
    public int updatePositionData(PositionData positionData)
    {
        return positionDataMapper.updatePositionData(positionData);
    }

    /**
     * 批量删除岗位标准
     *
     * @param standardPositions 需要删除的岗位标准主键
     * @return 结果
     */
    @Override
    public int deletePositionDataByStandardPositions(String[] standardPositions)
    {
        return positionDataMapper.deletePositionDataByStandardPositions(standardPositions);
    }

    /**
     * 删除岗位标准信息
     *
     * @param standardPosition 岗位标准主键
     * @return 结果
     */
    @Override
    public int deletePositionDataByStandardPosition(String standardPosition)
    {
        return positionDataMapper.deletePositionDataByStandardPosition(standardPosition);
    }

    /**
     * 根据标准岗位代码查询所有上升途径
     *
     * @param standardPositionCode 标准岗位代码
     * @return 上升途径列表
     */
    @Override
    public List<String> selectPromotionPathsByStandardPositionCode(String standardPositionCode)
    {
        List<String> resultList = new ArrayList<>();
        List<Map<String, Object>> list = positionDataMapper.selectPromotionPathsByStandardPositionCode(standardPositionCode);
        this.getPromotionPaths(list, resultList);
        return resultList;
    }

    private void getPromotionPaths(List<Map<String, Object>> list, List<String> resultList) {
        if (list == null){
            for (Map<String, Object> map : list) {
                if (map.get("promotionPath") != null){
                    String promotionPath = map.get("promotionPath").toString();
                    List<Map<String, Object>> newList = positionDataMapper.selectPromotionPathsByStandardPositionCode(promotionPath);
                    if (!newList.isEmpty()){
                        for (Map<String, Object> stringObjectMap : newList) {
                            if (stringObjectMap.get("standardPosition") != null){
                                String standardPosition = stringObjectMap.get("standardPosition").toString();
                                resultList.add(standardPosition);
                            }

                        }
                        this.getPromotionPaths(newList, resultList);
                    }
                }
            }
        }

    }
}
